<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/9/009
 * Time: 8:15
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
class Cub extends Controller
{
    public function list()//显示俱乐部列表方法
    {
        $this->checkLogin();
        //获取俱乐部列表
        $cub = Db::table('cub')->select();
        $this->assign('list', $cub);
        return $this->fetch('list');
    }

    public function add()//添加处理方法
    {
        $this->checkLogin();
        $request=input('');
        $pic=request()->file('cub_pic');
        $info = $pic->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'static'.DS.'admin'.DS.'img');
            $picname=$info->getFilename();
            $cub_admin = array_slice($request,-2);
            $request = array_slice($request,0,5);
            $cub_admin['password'] = md5($cub_admin['password']);
            $request['adminname'] = $cub_admin['adminname'];
            $request['cub_pic']=$picname;
            $sql1=Db::table('admin')->insert($cub_admin);
            $sql=Db::table('cub')->insert($request);
            
            if ($sql && $sql1){
                $this->success('增加成功','admin/cub/list');
            }
            else{
                $this->error('增加失败','admin/cub/list');
            }
    }

    public function addlist()//显示添加页面方法
    {
        $this->checkLogin();
        return $this->fetch('addlist');
    }

    public function delect()//删除俱乐部方法
    {
        $this->checkLogin();
        $request=input('cub_id');
        $sql=Db::table('cub')->where('cub_id',$request)->find();
        $pic=$sql["cub_pic"];
        $file=ROOT_PATH."public".DS."static".DS. 'admin'.DS.'img'.DS.$pic;
        if (file_exists($file)) {
            unlink($file);//删除文件
        }
        if (Db::table("cub")->delete($request)){
            $haveAdmin = Db::table('cub')->where('adminname',$sql['adminname'])->find();
            if($haveAdmin) {
                $this->success("删除成功");
            }
            Db::table('admin')->where('adminname',$sql['adminname'])->delete();
            $this->success("删除成功");
        }
        else{
            $this->error("删除失败");
        }
    }

}
